<?php


namespace Pay\Controller;

use Think\Controller;

class PostPayController extends Controller
{
    
    public function index()
    {
        $this->assign('order',unserialize(I('order')));
        $this->display();
    }
	// -------------- 银行返回处理---------

	public function payReturn() {
		header("Content-type:text/html;charset=utf-8");
		$BillModel = M('core_pay_bill');		    // 数据库初始化
		$OrderModel = M('core_pay_order');		    // 数据库初始化
		$PayRecordModel = M('core_pay_flow');		    // 数据库初始化
		$PayRecordModel->startTrans();
		$OrderModel->startTrans();
		$BillModel->startTrans();
		//接收银行返回
		$MERCHANTID = I('MERCHANTID');
		$POSID      = I('POSID'); 			// 商户柜台代码
		$ORDERID    = I('ORDERID'); 		// 订单号16012108010910915219
		$PAYMENT    = I('PAYMENT'); 		// 付款金额
		$PAYSTATE   = I('PAYSTATE'); 		// 付款状态
		$REMARK1    = I('REMARK1'); 		// 备注一
		$NotifyType = I('NotifyType');		// 返回方式
		$SIGN       = I('SIGN');			// 签名
		$PAYIP      = get_client_ip();		// 支付IP
		//写日志
		//验签元素准备
		
		$PERKEY = "zibodake"; 
        
		//服务器返回|页面返回参数不同
      	if($NotifyType== 1){
      		$sign='MERCHANTID='.$MERCHANTID.'&POSID='.$POSID.'&ORDERID='.$ORDERID.'&PAYMENT='.$PAYMENT.'&PAYSTATE='.$PAYSTATE.'&REMARK1='.$REMARK1.'&PERKEY='.$PERKEY ;
      	}else{
      		$sign='MERCHANTID='.$MERCHANTID.'&POSID='.$POSID.'&ORDERID='.$ORDERID.'&PAYMENT='.$PAYMENT.'&PAYSTATE='.$PAYSTATE.'&REMARK1='.$REMARK1.'&PAYIP='.$PAYIP.'&PERKEY='.$PERKEY ;
      	}
		//验签
		$strMD5 = MD5($sign);
		$content = 'MERCHANTID='.I('MERCHANTID').'   POSID='.I('POSID').'   ORDERID='.I('ORDERID').'   PAYMENT='.I('PAYMENT').'   PAYSTATE='.I('PAYSTATE').'   REMARK1='.I('REMARK1').'   NotifyType='.I('NotifyType').'   SIGN='.I('SIGN').'  date='.date("Y-m-d H:i:s").'perkey='.$key['perkey'].'strMD5='.$strMD5;
		file_put_contents("log.txt",$content.PHP_EOL,FILE_APPEND);
		if($strMD5==$SIGN){
			//更新流水，订单，账单
			$data['paystate'] = 1;
			$presult = $PayRecordModel->where('flow_id = '.$ORDERID)->save($data);    //流水
			$order_id = $PayRecordModel->where('flow_id = '.$ORDERID)->getField('order_id');
			$oresult = $OrderModel->where('order_id = '.$order_id['order_id'])->save($data);		  //订单
			$bresult = $BillModel->where('order_id = '.$order_id['order_id'])->save($data);		  //账单
			if($presult&&$oresult&&$bresult){
				$PayRecordModel->commit();
				$OrderModel->commit();
				$BillModel->commit();
				echo "<script>";
				echo"alert('支付成功，请去个人中心查看');";
				echo"history.go(-7);";
				echo"</script>";
				exit;
			}else{
				$PayRecordModel->rollback();
				$OrderModel->rollback();
				$BillModel->rollback();
				echo "<script>";
				echo"alert('支付失败');";
				echo"</script>";
				exit;
			}
		}else{
			echo "<script>";
			echo"alert('验签失败');";
			echo"</script>";
			exit;
		}
	}
	public function returnS($Model,$order){
		if($order['paystate']  	  ==1){
			echo "<script>";
			echo"alert('您已支付成功，请到个人中心查看');";
			echo"</script>";
			exit;
		}else{
			//更新订单状态：成功
			$data['paystate']	  = 1;
			$r 					  = $Model['Order']->where ("ordernumber ='". $order['ordernumber']."'" )->save($data);
			if($r){
				$Model['Order']->commit();
				$Model['p_l_o']->commit();
				echo "<script>";
				echo"alert('支付成功，请到个人中心查看');";
				echo"</script>";
				exit;
			}else{
				$Model['Order']->rollback();
				$Model['p_l_o']->rollback();
				echo "<script>";
				echo"alert('支付错误');";
				echo"</script>";
				exit;
			}
		}
	}
}